package net.huashitong.Indicators.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.huashitong.Indicators.dao.EarlyWarningDao;
import net.huashitong.Indicators.entity.EarlyWarning;
import net.huashitong.Indicators.entity.IndicatorBreakdown;
import net.huashitong.Indicators.filter.IndicatorBreakdownFilter;
import net.huashitong.Indicators.utils.IndustryNameUtils;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.sirdc.modules.core.dao.BaseDao;
import com.sirdc.modules.sys.base.StringPKService;
import com.sirdc.modules.sys.service.SysTableService;

@Service
public class EarlyWarningService extends StringPKService<EarlyWarning> {
	@Autowired
	private SysTableService tableService;
	@Autowired
	private EarlyWarningDao dao;
	@Autowired
	private IndicatorBreakdownService breakdownService;
	@Override
	protected BaseDao<EarlyWarning, String> getDao() {
		// TODO Auto-generated method stub
		return dao;
	}
	@Override
	protected void beforeSave(EarlyWarning earlyWarning) {
		earlyWarning.setSysId(tableService.updateMaxSysId("t_gis_earlyWarning", null));
	}
	public List<Map<String, Object>> queryByCreate() {
		List<EarlyWarning> earlyWarnings = dao.queryByCreate();
		List<Map<String, Object>> list = new ArrayList<Map<String,Object>>();
		List<IndicatorBreakdown> breakdowns = new ArrayList<IndicatorBreakdown>();
		for (int i = 0; i < earlyWarnings.size(); i++) {
			if(i==3){
				break;
			}
			IndicatorBreakdown breakdown = new IndicatorBreakdown();
			breakdown.setDate(earlyWarnings.get(i).getDate());
			breakdown.setArea(earlyWarnings.get(i).getArea());
			breakdown.setIndustryId(earlyWarnings.get(i).getIndicatorId());
			breakdowns.add(breakdown);
		}
		for (IndicatorBreakdown indicatorBreakdown : breakdowns) {
			IndicatorBreakdownFilter filter = new IndicatorBreakdownFilter();
			Map<String, Object> map = new HashMap<String, Object>();
			filter.setIndustryId(indicatorBreakdown.getIndustryId());
			filter = IndustryNameUtils.changeName(filter);
			List breakdown = breakdownService.queryAll(indicatorBreakdown.getDate(),filter.getIndustryName(),indicatorBreakdown.getArea());
			
			List breakdown2 = breakdownService.queryAllSec(indicatorBreakdown.getDate(),filter.getIndustryName(),indicatorBreakdown.getArea());
			Object[] object = (Object[]) breakdown.get(0);
			Object[] objectSec = (Object[]) breakdown2.get(0);
			map.put("industryId", object[0]);
			map.put("industryName", object[2]);
			map.put("rankRatio", object[4].toString());
			map.put("rankCumulative", objectSec[4].toString());
			list.add(map);
		}
		return list;
	}
}
